package com.sun.graphics2D;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class MakeImageUtils {
    public static String getMakeImagePath(Map<String,String> map) {
        File backFile = new File(map.get("backFile"));//背景图片
        String msg = map.get("msg");
        Color msgColor = new Color(0,0,0);
        FileOutputStream outImgStream = null;
        try {
            outImgStream = new FileOutputStream(map.get("path"));
            BufferedImage read = ImageIO.read(backFile);
            BufferedImage bufImg = modifyImagetogeter(read);
            int height = read.getHeight();
            int width = read.getWidth();
            Graphics2D graphics = bufImg.createGraphics();
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            graphics.drawImage(read,0,0,width,height,null);
            int hangwidth = width - 100;
            Font font = getDefinedFont((float) 18.0);
            graphics.setColor(msgColor);
            graphics.setFont(font);

            FontMetrics fontMetrics = graphics.getFontMetrics();
            int stringWidth = fontMetrics.stringWidth(msg);

            int hang = (int) Math.ceil(stringWidth * 1.0 / hangwidth);
            int fontHeight = (int) font.getSize2D();
            StringBuilder sb = new StringBuilder();
            int j = 0;
            int tempStart = 0;
            String tempStrs[] = new String[hang];

            for (int i1 = 0; i1 < msg.length(); i1++) {
                char ch = msg.charAt(i1);
                sb.append(ch);
                Rectangle2D bounds2 = fontMetrics.getStringBounds(sb.toString(), null);
                int tempStrPi1exlWi1dth = (int) bounds2.getWidth();
                if (tempStrPi1exlWi1dth > hangwidth) {
                    tempStrs[j++] = msg.substring(tempStart, i1);
                    tempStart = i1;
                    sb.delete(0, sb.length());
                    sb.append(ch);
                }
                if (i1 == msg.length() - 1) {// 最后一行
                    tempStrs[j] = msg.substring(tempStart);
                }
            }
            for (int i = 0; i < tempStrs.length; i++) {
                graphics.drawString(tempStrs[i], 50, (fontHeight + 5) * (i + 1));
            }

            ImageIO.write(bufImg,"png",outImgStream);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                outImgStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return null;
    }
    public static BufferedImage modifyImagetogeter(BufferedImage originalImage) {
        Graphics2D g = null;
        try {
            g = originalImage.createGraphics();
            g.setColor(new Color(100, 100, 100,160));
            g.fillRect(0,  originalImage.getHeight(), originalImage.getWidth(),originalImage.getHeight());
            g.dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return originalImage;
    }
    public static Font getDefinedFont(float fs) {
        Font font = null;
        String fontUrl="D:\\中科软信息\\需求\\A端投保成功页改造需求\\需求文档\\SourceHanSansCN-Medium.ttf";
        InputStream is = null;
        BufferedInputStream bis = null;
        try {
            is =new FileInputStream(new File(fontUrl));
            bis = new BufferedInputStream(is);
            font = Font.createFont(Font.TRUETYPE_FONT, is);
            //设置字体大小，float型
            font = font.deriveFont(fs);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != bis) {
                    bis.close();
                }
                if (null != is) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return font;
    }
    public static void main(String[] args) {
        Map<String,String> map = new HashMap<>();
        map.put("msg","         恭喜您购买成功！您购买的农银附加金穗投保人豁免保险费定期寿险等产品已经核保通过，投保单号E11662068,保费5000000.00元。我公司将尽快制作保险合同，并送达投保人。");
        map.put("backFile","D:\\中科软信息\\需求\\A端投保成功页改造需求\\需求文档\\分享图片.png");
        map.put("path","D:\\中科软信息\\需求\\A端投保成功页改造需求\\需求文档\\1.png");
        getMakeImagePath(map);
    }
}
